package com.demo.service;

import com.demo.vo.*;
import com.yiyi.JsonUtil;
import com.yiyi.StringUtil;
import com.yiyi.http.HttpClientUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author sdyang
 * @create 2019-02-22 9:24
 **/
@Service
public class PageService {

    protected static String URL = "http://120.79.200.233/openApi/invoke";

    public  static String queryTemplate(TemplatesParam param){

        param.setCode("123");
        param.setName("测试");
        param.setTitle("标题");

        ApiParam apiParam = new ApiParam();
        apiParam.setServiceName("com.wolf.querylist.service.impl.DefaultQueryListManager");
        apiParam.setMethodName("queryResult");

        List<ParamDetail> params = new ArrayList<>();

        ParamDetail detail1 = new ParamDetail();
        detail1.setParamName("key");
        detail1.setDataType("java.lang.String");
        detail1.setDataValue("query_template_list");
        params.add(detail1);

        Map<String,String> dataMap = new HashMap<>();
        if(StringUtil.isNotEmpty(param.getName())) {
            dataMap.put("namexxx128xxx", param.getName());
        }

        ParamDetail detail2 = new ParamDetail();
        detail2.setParamName("paramMap");
        detail2.setDataType("java.util.Map");

        detail2.setDataValue(JsonUtil.toJSONString(dataMap));
        params.add(detail2);

        apiParam.setParams(params);
        HashMap<String,String> data = new HashMap<>();
        System.out.println(JsonUtil.toJSONString(apiParam));
        data.put("data", JsonUtil.toJSONString(apiParam));
        String response = HttpClientUtil.post(URL,data);

        System.out.println(response);

        BaseResult result = JsonUtil.parseObject(response,BaseResult.class);
        return  result.getMessage();
    }

    public static void insert(OrderTemplate template){
        ApiParam apiParam = new ApiParam();
        apiParam.setServiceName("com.wolf.mock.Simulation");
        apiParam.setMethodName("startFlowAction");

        List<ParamDetail> paramDetails = new ArrayList<ParamDetail>();

        ParamDetail detail1 = new ParamDetail();
        detail1.setParamName("mockObject");
        detail1.setDataType("com.wolf.mock.MockObject");

        MockObject mockObject = new MockObject();
        mockObject.setClientId(100L);
        mockObject.setType("Save");
        mockObject.setFlowId(114L);
        mockObject.setTesting(true);

        HashMap<String,String> data = new HashMap<>();
        data.put("order_template_name","测试");
        data.put("order_template_code","编号");
        data.put("order_template_title","标题");
        data.put("order_template_head","页头");
        data.put("order_template_body","页面内容");

        mockObject.setParamMap(data);

        detail1.setDataValue(mockObject);

        paramDetails.add(detail1);
        apiParam.setParams(paramDetails);

        HashMap<String,String> paramMap = new HashMap<>();
        System.out.println(JsonUtil.toJSONString(apiParam));
        paramMap.put("data", JsonUtil.toJSONString(apiParam));
        String response = HttpClientUtil.post(URL,paramMap);
        System.out.println(response);
    }

    public  static void main(String[] args){
        insert(null);
    }
}
